/**
 * 
 */
package jnzb.control.uiUpdate;

import jnzb.control.queue.QueueManager;
import jnzb.model.nzb.NZB;
import jnzb.model.nzb.Segment;
import jnzb.model.queue.FinishedTableModel;
import jnzb.model.queue.WaitingTableModel;
import jnzb.ui.UserInterface;

/**
 * This whole class needs refactoring so that the GUI is notified of changes
 * then sucks the data from the appropriate models.<br>
 * Until then, this is a delegate class that fires data into the gui.
 * 
 * @author Rid
 * 
 */
public class UIUpdateDelegate {

	private UserInterface ui = null;

	public UIUpdateDelegate(UserInterface ui) {
		this.ui = ui;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jnzb.control.uiUpdate.UIUpdateDelegate#incrementNZBPercentage(long)
	 */
	public void incrementNZBPercentage(NZB nzb) {
		this.ui.getNzbPanel()
				.updatePercent((int) nzb.getProgressAsPercentage());
		this.ui.updateTitlePercentage(nzb.getProgressAsPercentage());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * jnzb.control.uiUpdate.UIUpdateDelegate#nzbFinished(jnzb.model.nzb.NZB)
	 */
	public void nzbFinished(NZB nzb) {
		this.ui.resetTitle();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jnzb.control.uiUpdate.UIUpdateDelegate#rememberInfo()
	 */
	public String getDisplayedText() {
		return this.ui.getInfoPanel().getDisplayedText();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jnzb.control.uiUpdate.UIUpdateDelegate#resetNZBDisplay()
	 */
	public void resetNZBDisplay() {
		this.ui.getNzbPanel().resetPanel();
		this.ui.getCurrentPanel().resetPanel();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * jnzb.control.uiUpdate.UIUpdateDelegate#setCurrentDownload(java.lang.String
	 * , int)
	 */
	public void setCurrentDownload(String subject, int numberOfSegments) {
		// Update the gui with what we're getting
		this.ui.getCurrentPanel().setFile(subject);
		// Set the range on the progress bar
		this.ui.getCurrentPanel().setNumberSegments(numberOfSegments);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * jnzb.control.uiUpdate.UIUpdateDelegate#setCurrentFileProgress(jnzb.model
	 * .nzb.Segment)
	 */
	public void setCurrentFileProgress(Segment segment) {
		this.ui.getCurrentPanel().setProgress(segment.getNumber());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * jnzb.control.uiUpdate.UIUpdateDelegate#setCurrentGroup(java.lang.String)
	 */
	public void setCurrentGroup(String group) {
		this.ui.getCurrentPanel().setGroup(group);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * jnzb.control.uiUpdate.UIUpdateDelegate#setCurrentSegmentSize(jnzb.model
	 * .nzb.Segment)
	 */
	public void setCurrentSegmentSize(Segment segment) {
		this.ui.getCurrentPanel().setSegmentSize(segment.getBytes());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * jnzb.control.uiUpdate.UIUpdateDelegate#setNZBDetails(java.lang.String,
	 * java.lang.String, long)
	 */
	public void setNZBDetails(String nzbName, String contains, long nzbSize) {
		this.ui.getNzbPanel().setNZBFilename(nzbName);
		this.ui.getNzbPanel().setContains(contains);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jnzb.control.uiUpdate.UIUpdateDelegate#setPauseDisplay(boolean)
	 */
	public void setPauseDisplay(boolean paused) {
		this.ui.getControlPanel().setPausedText(paused);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jnzb.control.uiUpdate.UIUpdateDelegate#setSegmentProgress(int)
	 */
	public void setSegmentProgress(int bytes) {
		this.ui.getCurrentPanel().setDownloaded(bytes);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jnzb.control.uiUpdate.UIUpdateDelegate#setSpeed(double)
	 */
	public void setSpeed(double speed) {
		this.ui.getCurrentPanel().setSpeed(speed);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * jnzb.control.uiUpdate.UIUpdateDelegate#updateTimeLeft(jnzb.model.nzb.NZB)
	 */
	public void updateTimeLeft(NZB nzb, double avgKPerS) {
		this.ui.getNzbPanel().setCompletionTime(nzb.getTimeLeftAt(avgKPerS));
	}

	/**
	 * Set the queue model in the appropriate panel/jtable
	 * 
	 * @param queueModel
	 */
	public void setWaitQueueModel(WaitingTableModel queueModel) {
		this.ui.getQueuePanel().getWaitingQueuePanel().setModel(queueModel);

	}

	/**
	 * Set the (nzb's) finished model in the appropriate panel/jtable
	 * 
	 * @param finishedModel
	 */
	public void setFinishedModel(FinishedTableModel finishedModel) {
		ui.getQueuePanel().getFinishedPanel().setModel(finishedModel);
	}

	public void disableQueueControlButtons() {
		ui.getQueuePanel().getWaitingQueuePanel().disableButtons();
	}

	public void enableQueueControlButtons() {
		ui.getQueuePanel().getWaitingQueuePanel().enableButtons();
	}

	/**
	 * Completely hide the entire GUI
	 */
	public void hideGui() {
		ui.setVisible(false);
		ui.getInfoPanel().hideEventFrame();
		ui.getQueuePanel().hideQueueControlFrame();
	}

	/**
	 * Set the queue manager in the queue panel so it can read the size when it
	 * indicates change
	 * 
	 * @param queueManager
	 */
	public void setQueueManager(QueueManager queueManager) {
		ui.getQueuePanel().setQueueManager(queueManager);

	}

	/**
	 * Set the hilighted entry on the queue table
	 * @param newPos
	 */
	public void setSelectedQueueEntry(int newPos) {
		ui.getQueuePanel().getWaitingQueuePanel().setSelectedRow(newPos);
		
	}

}
